//
//  SWAssetCollectionCell.swift
//  MiU
//
//  Created by linhan on 16/3/28.
//  Copyright © 2016年 MiU. All rights reserved.
//

import Foundation
import UIKit
class SWAssetCollectionCell: UICollectionViewCell,SWALPhotoDelegate
{
    weak var photo:SWALPhoto? {
        didSet {
            photo?.delegate = self
            if _inited && _change
            {
                layoutSubviews()
            }
        }
    }
    weak var delegate:CollectionViewCellDelegate?
    
    private var _change:Bool = true
    
    private var _inited:Bool = false
    
    private var _thumbImageView:UIImageView = UIImageView()
    
    private var _selectedBtn:UIButton = UIButton(type: .system)
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setup()
    }
    
    deinit
    {
        //trace("DEINIT SWAssetCollectionCell")
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        _thumbImageView.frame = CGRectMake(0, 0, self.width, self.height)
        _thumbImageView.contentMode = .scaleAspectFill
        _thumbImageView.clipsToBounds = true
        contentView.addSubview(_thumbImageView)
        
        _selectedBtn.setBackgroundImage(UIImage(named:"child_radio_normal"), for: .normal)
        _selectedBtn.setBackgroundImage(UIImage(named:"child_radio_selected"), for: .selected)
        _selectedBtn.sizeToFit()
        _selectedBtn.frame = CGRectMake(width - _selectedBtn.width - 2, 2, _selectedBtn.width, _selectedBtn.height)
        _selectedBtn.addTarget(self, action: #selector(selectedBtnTapped), for: .touchUpInside)
        contentView.addSubview(_selectedBtn)
        
        let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
        contentView.addGestureRecognizer(longPressGesture)
        
        _inited = true
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    func photoThumbChanged(_ photo:SWALPhoto, thumb:UIImage)
    {
        _thumbImageView.image = thumb
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        if let photo = photo , _change
        {
            _change = false
            _thumbImageView.image = nil
            _selectedBtn.isSelected = photo.selected
            if let thumb = photo.thumbnail
            {
                _thumbImageView.image = thumb
            }
        }
    }
    
    @objc private func longPressed()
    {
        delegate?.collectionViewCell(self, click: CallbackAction.LongPress, argument: nil)
    }
    
    @objc private func selectedBtnTapped()
    {
        if _selectedBtn.isSelected
        {
            delegate?.collectionViewCell(self, click: CallbackAction.Deselect, argument: nil)
        }
        else
        {
            delegate?.collectionViewCell(self, click: CallbackAction.Select, argument: nil)
        }
    }
    
    
}
